package ${packagePath};

import com.github.pagehelper.PageInfo;
import com.ibp.enums.BusinessType;
import com.ibp.domain.annotation.Log;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ist.base.BaseController;
import com.ist.api.ApiResult;
<#if imports??>
<#list imports as import>
import ${import};
</#list>
</#if>

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
 * ${comment}
 * @date ${.now?string("yyyy-MM-dd HH:mm:ss")}
 * @author ${author}
 */
@Api(tags = "${comment}")
@RestController
@RequestMapping("/${projectName}/${simpleUrl}")
public class ${simpleName} extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(${simpleName}.class);

    @Resource
    private ${simpleService} ${lowerSimpleService};

    /**
     * 查询分页数据
     * @param ${business.dao.lowerModelName} 入参
     * @return 数据结果集
     */
    @ApiOperation("分页查询数据")
    @PostMapping(value = "/page")
    @Log(title = "查询${comment}分页数据", businessType = BusinessType.SELECT)
    public ApiResult<PageInfo<${business.dao.modelName}>> getPage(@RequestBody ${business.dao.modelName} ${business.dao.lowerModelName}){
        startPage();
        return ApiResult.ok(${lowerSimpleService}.getPage(${business.dao.lowerModelName}));
    }

<#list business.dao.model.fields as field>
    <#if field.id>
    /**
     * 新增数据
     * @param ${business.dao.lowerModelName} 入参
     * @return 结果响应
     */
    @ApiOperation("新增数据")
    @PostMapping(value = "/")
    @Log(title = "新增${comment}数据", businessType = BusinessType.INSERT)
    public ApiResult<Boolean> addData(@RequestBody ${business.dao.modelName} ${business.dao.lowerModelName}){
        try{
            ${lowerSimpleService}.addData(${business.dao.lowerModelName});
        } catch (Exception e) {
            logger.error("${comment}新增失败!", e);
            return ApiResult.fail("新增失败!");
        }
        return ApiResult.ok(true, "新增成功!");
    }

    /**
     * 根据主键查询数据
     * @param id 主键ID
     * @return 数据结果集
     */
    @ApiOperation("根据主键查询数据")
    @GetMapping(value = "/{id}")
    @Log(title = "根据主键查询${comment}数据", businessType = BusinessType.SELECT)
    public ApiResult<${business.dao.modelName}> getDataById(@PathVariable Long id){
        return ApiResult.ok(${lowerSimpleService}.getDataById(id));
    }

    /**
     * 根据主键更新数据
     * @param id 主键ID
     * @param ${business.dao.lowerModelName} 入参
     * @return 结果响应
     */
    @ApiOperation("根据主键更新数据")
    @PutMapping(value = "/{id}")
    @Log(title = "根据主键更新${comment}数据", businessType = BusinessType.UPDATE)
    public ApiResult<Boolean> updateData(@PathVariable Long id, @RequestBody ${business.dao.modelName} ${business.dao.lowerModelName}){
        try{
            ${lowerSimpleService}.updateData(id, ${business.dao.lowerModelName});
        } catch (Exception e) {
            logger.error("${comment}更新失败!", e);
            return ApiResult.fail("更新失败!");
        }
        return ApiResult.ok(true, "更新成功!");
    }

    /**
     * 根据主键删除数据
     * @param id 主键ID
     * @return 结果响应
     */
    @ApiOperation("根据主键删除数据")
    @DeleteMapping(value = "/{id}")
    @Log(title = "根据主键删除${comment}数据", businessType = BusinessType.DELETE)
    public ApiResult<Boolean> deleteDataById(@PathVariable Long id){
        try{
            ${lowerSimpleService}.deleteDataById(id);
        } catch (Exception e) {
            logger.error("${comment}删除失败!", e);
            return ApiResult.fail("删除失败!");
        }
        return ApiResult.ok(true, "删除成功!");
    }

    </#if>
</#list>

}